
package codeGenerate.common.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.extra.pinyin.PinyinUtil;

public class SystemUtils {

	public static final String NAME_SPLIT = "_|-";

	private static final List<String> targetNameFilterRegexList = new ArrayList<String>();

	static {
		//不包含下划线，下划线用于名称分割
		targetNameFilterRegexList.add("[\\n`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？＞≥]");
	}

	/**
	 * @description: 根据代码路径生成默认实体类的名称
	 * @return
	 */
	public static String getBeanNameByPath(String path) {
		String[] split = path.split("\\.");
		String name = split[split.length - 1];
		return name.substring(0, 1).toUpperCase() + name.substring(1);
	}

	/**
	 * @description: 获取模板默认的路径
	 * @return
	 */
	public static String defalutTemplateDirectory() {
		return SystemUtils.class.getResource("/").getPath() + "template";
	}

	/**
	 * @description: 大写第一个字母
	 * @return
	 */
	public static String upperFirst(String str) {
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}

	/**
	 * @description: 将名字变为类名,如有下划线，自动去除，首字母变大写
	 * @creator: dw-fu1
	 * @createDate: 2021年4月29日
	 * @param name
	 * @return
	 */
	public static String toClassName(String name) {
		String[] names = name.split(NAME_SPLIT);
		if (names.length == 1) {
			String className = names[0];
			return className.substring(0, 1).toUpperCase() + className.substring(1);
		}
		StringBuffer sb = new StringBuffer();
		sb.append(names[0].toLowerCase());
		for (int i = 1; i < names.length; i++) {
			if (StringUtils.isNotBlank(names[i])) {
				sb.append(names[i].substring(0, 1).toUpperCase());
				sb.append(names[i].substring(1).toLowerCase());
			}
		}
		return sb.substring(0, 1).toUpperCase() + sb.substring(1);
	}

	public static String toClassFieldName(String name) {
		String[] names = name.split(NAME_SPLIT);
		if (names.length == 1) {
			String fieldName = names[0];
			return fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);
		}
		StringBuffer sb = new StringBuffer();
		sb.append(names[0].toLowerCase());
		for (int i = 1; i < names.length; i++) {
			if (StringUtils.isNotBlank(names[i])) {
				sb.append(names[i].substring(0, 1).toUpperCase());
				sb.append(names[i].substring(1).toLowerCase());
			}
		}
		return sb.substring(0, 1).toLowerCase() + sb.substring(1);
	}

	/**
	 * 数据库底层的列名转换成属性名称，默认峰驼命名
	 */
	public static String dbColumnNameNameToJavaPropertyName(String dbColumnName) {
		String[] names = dbColumnName.split("_");
		StringBuffer sb = new StringBuffer();
		sb.append(names[0].toLowerCase());
		for (int i = 1; i < names.length; i++) {
			if (StringUtils.isNotBlank(names[i])) {
				sb.append(names[i].substring(0, 1).toUpperCase());
				sb.append(names[i].substring(1).toLowerCase());
			}
		}
		return sb.toString();
	}

	/**
	 * 中文名转化成类的字段名
	 */
	public static String chineseToClassFieldName(String chinese) {
		//替换掉特殊字符
		for (String reg : targetNameFilterRegexList) {
			chinese = chinese.replaceAll(reg, "");
		}
		//将输入字符串转为拼音首字母，其它字符原样返回
		String fieldName = PinyinUtil.getFirstLetter(chinese, "");
		return dbColumnNameNameToJavaPropertyName(fieldName);
	}

	/**
	 * 中文名转化成数据库列编码
	 */
	public static String chineseToDbColumnCode(String chinese) {
		//替换掉特殊字符
		for (String reg : targetNameFilterRegexList) {
			chinese = chinese.replaceAll(reg, "");
		}
		//将输入字符串转为拼音首字母，其它字符原样返回
		String code = PinyinUtil.getFirstLetter(chinese, "");
		//将编码转换大写
		return code.toUpperCase();
	}

	public static void logIterate(String content, Iterable iterable) {
		System.out.println(String.format("===================%s===================", content));
		iterable.forEach(System.out::println);
		System.out.println(String.format("===================%s===================", content));
	}

	public static void main(String[] args) {
		System.out.println(chineseToClassFieldName("aa你好"));
	}

}
